/*
	DS Kanji Flash
	Copyright (C) 2009,2010 Bernhard Schelling

	This file is part of DS Kanji Flash.

	DS Kanji Flash is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	DS Kanji Flash is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with DS Kanji Flash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "global.h"
#include "dict.h"

static sRect rbtnOrder(20,20,216,25), rbtnDraw(20,65,216,25), rbtnClose(20,155,216,25);
//static sRect rbtnKana(20,110,216,25);
static bool bLastPressedL, bMenu = false;
static UL_IMAGE *imgKanji = NULL;
static int iKanji = -1;

class cSceneCard : public cScene
{
public:
	cSceneCard() : cScene(SCN_CARD)
		{ WantsKeyReleaseEvents = true; }


	void load()
	{
		ulSetLoadUtilitySource(VF_MEMORY);
		if (imgKanji) ulDeleteImage(imgKanji);
		imgKanji = ulLoadImageFilePNG((char*)kanjipool_png[iKanji], (int)(kanjipool_png[iKanji+1]-kanjipool_png[iKanji]), UL_IN_VRAM, UL_PF_PAL4);
	}
	
	void next()
	{
		if (++iKanji >= kanjipool_num) iKanji = 0;
		load();
	}
	
	void prev()
	{
		if (--iKanji < 0) iKanji = kanjipool_num-1;;
		load();
	}

	virtual void Enter(void *p1, void *p2, void *p3)
	{
		if (p1) { iKanji = (int)p1-1; next(); }
		else if (iKanji == -1) next();
	}

	virtual void ProcessInput()
	{
		if (ul_keys.pressed.left||ul_keys.pressed.up||ul_keys.pressed.X||ul_keys.pressed.Y) prev();
		if (ul_keys.pressed.right||ul_keys.pressed.down||ul_keys.pressed.A||ul_keys.pressed.B) next();
		if (ul_keys.pressed.L) bLastPressedL = true;
		if (ul_keys.pressed.R) bLastPressedL = false;
		if (ul_keys.pressed.touch && !bMenu) bMenu = true;
		else if (ul_keys.pressed.touch)
		{
			bMenu = false;
			if (rbtnOrder.touchButton()) cScene::GoTo(SCN_SHOWSTROKES, (void*)iKanji);
			else if (rbtnDraw.touchButton()) cScene::GoTo(/*SCN_DRAW*/SCN_TRAIN, (void*)iKanji);
			//else if (rbtnKana.touchButton()) cScene::GoTo(SCN_RECORD);
		}
	}
	
	virtual void DrawScreen_Top()
	{
		ulDrawGradientRect(0, 0, 256, 192, RGB15(25, 25, 25), RGB15(25, 25, 25), RGB15(31, 31, 31), RGB15(31, 31, 31));
		if (ul_keys.held.L || ul_keys.held.R)
		{
			ulDrawFillRect(10-2, 10-2, 10+64+2, 10+64+2, COL_BLACK);
			imgKanji->stretchX = imgKanji->stretchY = 64;
			ulDrawImageXY(imgKanji, 10, 10);
			const char *chrKanji = kanji[iKanji];
			while (*chrKanji != ' ') chrKanji++;
			int iKun = 0, iOn = 0, iMeaning1 = 0, iMeaning2 = 0;
			while (*(chrKanji++)!='\n')
			{
				const char *chrDotSplit = NULL, *chrNext = chrKanji;
				wchar_t c;
				bool bKun = false, bOn = false, bMeaning = (*chrKanji == '{' ? chrKanji++ : false);
				while (bMeaning ? *chrNext!='}' : *chrNext!=' '&&*chrNext!='\n')
				{
					c = GetUCS(&chrNext);
					if      (c>=0x3000&&c<0x30A0) bKun = true;
					else if (c>=0x30A0&&c<0x3100) bOn = true;
					else if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')) bMeaning = true; 
					else if (c=='.') chrDotSplit = chrNext-1;
				}
				if (bKun || bOn)
				{
					int *x = (bKun ? &iKun : &iOn), y = (bKun ? 15 : 40);
					*x += 13+displayStringWidth(fntJapanese, chrKanji, 90+*x, y, COL_WHITE, (chrDotSplit?chrDotSplit:chrNext)-chrKanji);
					if (chrDotSplit) *x += displayStringWidth(fntJapanese, chrDotSplit+1, 90-13+*x, y, RGB15(15,15,31), chrNext-chrDotSplit-1);
				}
				if (bMeaning && iMeaning1<=iMeaning2) iMeaning1 += 20+displayStringWidth(ul_lcdFont, chrKanji, 10+iMeaning1, 85, COL_WHITE, chrNext-chrKanji);
				else if (bMeaning)                    iMeaning2 += 20+displayStringWidth(ul_lcdFont, chrKanji, 10+iMeaning2, 105, COL_WHITE, chrNext-chrKanji);
				//char bla[64];
				//int w = displayStringWidth(fntJapanese, chrKanji, 10, (x++)*20, COL_WHITE, chrNext-chrKanji);
				//sprintf(bla, "Kun:%d,On=%d,Meaning=%d,i=%d,e=%d,hcrOn=%d", bKun,bOn,bMeaning,chrKanji-kanji[iKanji],chrNext-kanji[iKanji],chrOn-chrKanji);
				//displayStringWidth(ul_lcdFont,bla,0,(x++)*15);
				chrKanji = (bMeaning&&*chrNext=='}'?chrNext+1:chrNext);
			}
		}
		else
		{
			ulDrawFillRect((256-128)/2-2, (192-128)/2-2, 256-(256-128)/2+2, 192-(192-128)/2+2, COL_BLACK);
			imgKanji->stretchX = imgKanji->stretchY = 128;
			ulDrawImageXY(imgKanji, (256-128)/2, (192-128)/2);
		}
		char chrNum[14];
		sprintf(chrNum, "%d/%d", iKanji+1, kanjipool_num);
		displayStringRight(ul_lcdFont, chrNum, 250, 180);
	}
	
	virtual void DrawScreen_Bottom()
	{
		ulDrawGradientRect(0, 0, 256, 192, RGB15(31, 31, 31), RGB15(31, 31, 31), RGB15(25, 25, 25), RGB15(25, 25, 25));
		for (int i = 0; i < 6; i ++)
		{
			const char* chrDict = GetDict(iKanji, i);
			if (!chrDict) break;
			char chrCompound[64]; int iCompoundLen = 0, cx = 27, cy = 6+38*i;
			sprintf(chrCompound, "%d. ", i+1);
			displayString(fntJapanese, chrCompound, 7, cy, COL_WHITE);
			if ((ul_keys.held.L && !ul_keys.held.R) || (ul_keys.held.L && ul_keys.held.R && bLastPressedL) || (!ul_keys.held.R))
			{
				while (*chrDict != ' ' && iCompoundLen<63) chrCompound[iCompoundLen++] = *(chrDict++);
				chrCompound[iCompoundLen] = 0;
				displayString(fntJapanese, chrCompound, cx, cy, COL_WHITE);				
			}
			if ((ul_keys.held.L && !ul_keys.held.R) || (ul_keys.held.L && ul_keys.held.R && bLastPressedL))
			{
				iCompoundLen = 0;
				while (*(chrDict++) != '[');
				while (*chrDict != ']' && iCompoundLen<63) chrCompound[iCompoundLen++] = *(chrDict++);
				chrCompound[iCompoundLen] = 0;
				displayString(fntJapanese, chrCompound, cx, cy + 18, COL_YELLOW);
			}
			else if (ul_keys.held.R)
			{
				ulDrawFillRect(25, cy - 2, 250, cy + 31, RGB15(15,15,15));
				bDrawFontShadows = false;
				int iText1 = 0, iText2 = 0, iText3 = 0;
				while (*(chrDict++) != '/');
				while (*chrDict != '\n')
				{
					for (iCompoundLen=0; *chrDict!='/'&&*chrDict!='\n'&&iCompoundLen<63; chrCompound[iCompoundLen++] = *(chrDict++));
					chrCompound[iCompoundLen] = 0;
					if (iText1<=iText2&&iText1<=iText3) iText1 += 15+displayStringWidth(ul_lcdFont, chrCompound, cx+iText1, 01+cy, COL_WHITE);
					else if (iText2<=iText3)            iText2 += 15+displayStringWidth(ul_lcdFont, chrCompound, cx+iText2, 11+cy, COL_WHITE);
					else                                iText3 += 15+displayStringWidth(ul_lcdFont, chrCompound, cx+iText3, 21+cy, COL_WHITE);
					if (*chrDict == '/') chrDict++;
				}
				bDrawFontShadows = true;
			}
		}
		if (bMenu)
		{
			drawButtonBG(rbtnOrder, BUTTON_BG_RED, "Stroke Order");
			drawButtonBG(rbtnDraw, BUTTON_BG_RED, "Draw Kanji");
			//drawButtonBG(rbtnKana, BUTTON_BG_RED, "Record Kana");
			drawButtonBG(rbtnClose, BUTTON_BG_BLUE, "Close Menu");
		}
	}
} SceneCard;
